/**
 * 消息表数据模型
 * @type {Message}
 */
/**********************************************************/
module.exports = class Message extends require('./model'){
  /**
   * 添加消息至数据库
   * @param message
   * @returns {Promise<unknown>}
   */
  static setMessage(message) {
    return new Promise((resolve, reject) => {
      let sql = 'INSERT INTO message SET ?'

      console.log('添加消息sql=' + sql);
      console.log('username=' + message.user_username + '; title=' + message.title + '; content=' + message.content +
        ';----------------------------------------');

      this.dbQuery(sql, message)
        .then(result => {
          resolve(result.insertId)
        })
        .catch(error => {
          reject(error)
        })
    })
  }

  /**
   * 删除消息记录
   * @param id
   * @returns {Promise<unknown>}
   */
  static deleteMessage(id) {
    return new Promise((resolve, reject) => {
      let sql = 'DELETE FROM message WHERE id = ?'

      console.log('删除消息sql=' + sql);
      console.log('id=' + id + ';---------------------------------------------');

      this.dbQuery(sql, id)
        .then(result => {
          resolve(result.affectedRows)
        })
        .catch(error => {
          console.log('删除消息失败！！！');
          reject(error)
        })
    })
  }

  /**
   * 获取指定消息的条数
   * @param username
   * @param handle
   * @returns {Promise<unknown>}
   */
  static getMessageCount(username, handle) {
    return new Promise((resolve, reject) => {
      let sql = `SELECT COUNT(1) AS count FROM message WHERE 1 = 1 `
      sql += username && username != '' ? ` AND message.user_username = '${username}' ` : ` `
      sql += handle && handle != 0 ? ` AND message.handle = ${handle} ` : ` `

      console.log('查询消息条数sql=' + sql);
      console.log('username=' + username + '; handle=' + handle + ';------------------------------------------------');

      this.dbQuery(sql)
        .then(result => {
          resolve(result[0])
        })
        .catch(error => {
          console.log('查询消息条数失败！！！');
          reject(error)
        })
    })
  }

  /**
   * 获取指定消息条数的信息
   * @param start
   * @param size
   * @param username
   * @param handle
   * @returns {Promise<unknown>}
   */
  static getMessage(start, size, username, handle) {
    return new Promise((resolve, reject) => {
      let sql = `SELECT * FROM message WHERE 1 = 1 `
      sql += username && username != '' ? ` AND message.user_username = '${username}' ` : ` `
      sql += handle && handle != 0 ? ` AND message.handle = ${handle} ` : ` `

      sql += ` ORDER BY message.handle DESC LIMIT ?, ? `

      console.log('查询消息sql=' + sql);
      console.log('start=' + start + '; size=' + size + '; username=' + username + '; handle=' + handle +
        ';---------------------------------------------------');

      this.dbQuery(sql, [start, size])
        .then(result => {
          resolve(result)
        })
        .catch(error => {
          console.log('查询指定条数消息信息失败！！！');
          reject(error)
        })
    })
  }

  /**
   * 更新消息
   * @param message
   * @returns {Promise<unknown>}
   */
  static updateMessage(message) {
    return new Promise((resolve, reject) => {
      let sql = `UPDATE message SET message.user_username = '${message.user_username}', message.title = '${message.title}', 
        message.content = '${message.content}', message.handle = ${message.handle} WHERE message.id = ${message.id} `
      console.log('修改消息sql=' + sql);
      console.log('username=' + message.user_username + '; title=' + message.title + '; content=' +
        message.content + '; handle=' + message.handle + '; id=' + message.id +
        ';---------------------------------------------------------------------------');

      this.dbQuery(sql)
        .then(result => {
          resolve(result.affectedRows)
        })
        .catch(error => {
          console.log('更新消息失败！！！');
          reject(error)
        })
    })
  }

}